package controller;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JViewport;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.*;


public class Settings extends JViewport{
	
	private JPanel rabbit;
	private JPanel fox;
	private JPanel wolf;
	private JPanel bear;
	
	private Map<Class, ImageIcon> images;
	
	public Settings() {
		super();
		
		JPanel inner = new JPanel();
		images = new LinkedHashMap<Class, ImageIcon>();
		images.put(Rabbit.class, new ImageIcon("res/rabbit.jpg"));
		images.put(Fox.class, new ImageIcon("res/fox.jpg"));
		images.put(Wolf.class, new ImageIcon("res/wolf.jpg"));
		images.put(Bear.class, new ImageIcon("res/bear.jpg"));
		
		inner.setLayout(new BoxLayout(inner, BoxLayout.X_AXIS));
		
		rabbit = new AnimalSettings(new Rabbit(), images.get(Rabbit.class));
		fox = new AnimalSettings(new Fox(), images.get(Fox.class));
		wolf = new AnimalSettings(new Wolf(), images.get(Wolf.class));
		bear = new AnimalSettings(new Bear(), images.get(Bear.class));
		
		
		JPanel more = new JPanel();
		more.setLayout(new BoxLayout(more, BoxLayout.Y_AXIS));
		JLabel disease = new JLabel("Disease");
		JLabel disPerc = new JLabel("Percentage");
		JLabel grass = new JLabel("Grass");
		JLabel grasRe = new JLabel("Regrowth rate");
		JLabel road = new JLabel("Road");
		JLabel roadTr = new JLabel("Traffic level");
		
		JSlider disSlider = new JSlider(0, 100, (int) (Rabbit.getDiseasePercentage()*100));
		disSlider.setMajorTickSpacing(50);
		disSlider.setMinorTickSpacing(10);
		disSlider.setPaintTicks(true);
		disSlider.setPaintLabels(true);
		disSlider.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider) e.getSource();
				Rabbit.setDiseasePercentage(((double)source.getValue())/100.0);
			}
		});
		
		JSlider grassSlider = new JSlider(0, 10, ModifierGrass.getRegrowthRate());
		grassSlider.setMajorTickSpacing(5);
		grassSlider.setMinorTickSpacing(1);
		grassSlider.setPaintTicks(true);
		grassSlider.setPaintLabels(true);
		grassSlider.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider) e.getSource();
				ModifierGrass.setRegrowthRate(source.getValue());
			}
		});

		JSlider roadSlider = new JSlider(0, 100, (int)(ModifierRoad.getTrafficLevel()*100));
		roadSlider.setMajorTickSpacing(50);
		roadSlider.setMinorTickSpacing(10);
		roadSlider.setPaintTicks(true);
		roadSlider.setPaintLabels(true);
		roadSlider.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider) e.getSource();
				ModifierRoad.setTrafficLevel(((double)source.getValue())/100.0);
			}
		});
		
		more.add(disease);
		more.add(disPerc);
		more.add(disSlider);
		more.add(grass);
		more.add(grasRe);
		more.add(grassSlider);
		more.add(road);
		more.add(roadTr);
		more.add(roadSlider);
		
		inner.add(rabbit);
		inner.add(fox);
		inner.add(wolf);
		inner.add(bear);
		inner.add(more);
		add(inner);
		
		setVisible(true);
		
	}

}
